package com.csd.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.csd.activity.EnterpriseApplayActivity;
import com.csd.activity.R;
import com.csd.bean.EnterpriseOrder;
import com.csd.fragment.AuditingFragment;

public class ApplayAdapter extends BaseAdapter {

	private Context context;
	private List<EnterpriseOrder> list;
	
	public ApplayAdapter(Context context, List<EnterpriseOrder> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		ViewHolder holder;
		if(convertView == null){
			view = LayoutInflater.from(context).inflate(R.layout.applay_item, null);
			holder = new ViewHolder();
			TextView orderId = (TextView) view.findViewById(R.id.orderid);
			TextView purposer = (TextView) view.findViewById(R.id.purpose);
			TextView carType = (TextView) view.findViewById(R.id.cartype);
			TextView time = (TextView) view.findViewById(R.id.time);
			TextView applayTime = (TextView) view.findViewById(R.id.applaytime);
			Button bt_cancle = (Button) view.findViewById(R.id.canle_order);
			Button bt_change = (Button) view.findViewById(R.id.change);
			bt_change.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				Intent intent = new Intent(context, EnterpriseApplayActivity.class);
				context.startActivity(intent);
				((Activity)context).finish();
				}
			});
			bt_cancle.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AuditingFragment.ll_cancle.setVisibility(View.VISIBLE);
					DisplayMetrics dm = new DisplayMetrics();
					dm = context.getApplicationContext().getResources()
							.getDisplayMetrics();
					int screenHeight = dm.heightPixels;
					AnimationSet animationSet = new AnimationSet(true);
					TranslateAnimation animation = new TranslateAnimation(0,
							0, screenHeight, 0);
					animation.setDuration(1000);
					animationSet.addAnimation(animation);
					AuditingFragment.ll_cancle.startAnimation(animationSet);
					
				}
			});
			holder.tv_orderId = orderId;
			holder.tv_purpose = purposer;
			holder.tv_cartype = carType;
			holder.tv_time = time;
			holder.tv_applayTime = applayTime;
			holder.bt_cancle = bt_cancle;
			holder.bt_change = bt_change;
			view.setTag(holder);
		}else{
			view = convertView;
			holder = (ViewHolder) view.getTag();
		}
		EnterpriseOrder order = list.get(position);
		holder.tv_orderId.setText("申请单：SQ-000"+order.getOrderId());
		holder.tv_purpose.setText(order.getPurpose());
		holder.tv_time.setText(order.getTakeTime()+"-"+order.getRetrunTime());
		holder.tv_cartype.setText(order.getCar().getCarBrand());
		holder.tv_applayTime.setText(order.getApplyTime());
		return view;
	}

	public class ViewHolder{
		private TextView tv_orderId,tv_purpose,tv_cartype,tv_time,tv_applayTime;
		private Button bt_cancle,bt_change;
	}
}
